<?php
/**
 * Visi Db Table Row Exception
 *
 * @package     Visi
 * @subpackage  Models
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2010 Luke Visinoni <luke.visinoni@gmail.com>
 * @version     $Id$
 */
class Visi_Db_Table_Row extends Zend_Db_Table_Row {

    /**
     * Modifies a string to remove all non ASCII characters and spaces.
     */
    static public function slugify($text) {

        // replace non letter or digits by -
        $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
        
        // trim
        $text = trim($text, '-');
        
        // transliterate
        if (function_exists('iconv')) {
            $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
        }
        
        // lowercase
        $text = strtolower($text);
        
        // remove unwanted characters
        $text = preg_replace('~[^-\w]+~', '', $text);
        
        if (empty($text)) {
            return false;
        }
        
        return $text;
    
    }
    
    protected function _insert() {
    
        if (array_key_exists('modified', $this->_data)) {
            $this->modified = new Zend_Db_Expr('NOW()');
        }
        if (array_key_exists('created', $this->_data)) {
            $this->created = new Zend_Db_Expr('NOW()');
        }
        if (!$this->validate()) {
            throw with(new Visi_Db_Table_Row_ValidationException(ucfirst($this->getTable()->info(Visi_Db_Table::NAME)) . " model failed to pass validation and cannot be created."))
                ->setRow($this)
                ->setErrors($this->getTable()->getRules()->getErrors());
        }
        return parent::_insert();
    
    }

    protected function _update() {
    
        if (array_key_exists('modified', $this->_data)) {
            $this->_data['modified'] = new Zend_Db_Expr('NOW()');
        }
        if (!$this->validate()) {
            throw with(new Visi_Db_Table_Row_ValidationException(ucfirst($this->getTable()->info(Visi_Db_Table::NAME)) . " model failed to pass validation and cannot be updated."))
                ->setRow($this)
                ->setErrors($this->getTable()->getRules()->getErrors());
        }
        return parent::_update();
    
    }
    
    public function filter() {
    
        $filters = $this->getTable()->getFilters();
        $data = $filters->filter($this->toArray());
        $this->setFromArray($data);
    
    }
    
    public function validate() {
    
        $this->filter();
        $rules = $this->getTable()->getRules()->setRow($this);
        return $rules->validate($this->toArray());
    
    }
    
    public function getCreatedTimestamp() {
    
        if (array_key_exists('created', $this->_data)) {
            $timestamp = strtotime($this->created);
            return $timestamp;
        }
        return null;
    
    }
    
    public function getModifiedTimestamp() {
    
        if (array_key_exists('modified', $this->_data)) {
            $timestamp = strtotime($this->modified);
            return $timestamp;
        }
        return null;
    
    }

}